package com.tiancan.design.user.service.impl;

import com.tiancan.design.user.bean.Result;
import com.tiancan.design.user.bean.pojo.User;
import com.tiancan.design.user.dao.UserDao;
import com.tiancan.design.user.service.CodeServiceFeignClient;
import com.tiancan.design.user.service.TokenService;
import com.tiancan.design.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;

/** @author tiancan */
@Service
public class UserServiceImpl implements UserService {
  @Autowired private UserDao userDao;
  @Autowired private CodeServiceFeignClient codeServiceFeignClient;
  @Autowired private TokenService tokenService;
  private static final Integer TIME_OUT_RESULT = 2;

  @Override
  public Result<String> register(String email, String password, String code) {
    Result<String> result = new Result<>();
    int res = codeServiceFeignClient.validate(email, code);
    if (res == 1) {
      result.setResult(false);
      result.setRes("验证码错误");
      return result;
    }
    if (res == TIME_OUT_RESULT) {
      result.setResult(false);
      result.setRes("系统超时,请稍后再试");
      return result;
    }
    Result<String> registered = isRegistered(email);
    if (registered.getResult()) {
      result.setResult(false);
      result.setRes(registered.getRes());
      return result;
    }
    try {
      User user = new User(email, password);
      userDao.save(user);
    } catch (Exception ex) {
      ex.printStackTrace();
      result.setResult(false);
      result.setRes("服务异常,请稍后再试");
      return result;
    }
    result.setResult(true);
    result.setData(tokenService.getToken(email + password));
    tokenService.storageToken(email, result.getData());
    return result;
  }

  @Override
  public Result<String> isRegistered(String email) {
    User user;
    Result<String> result = new Result<>();
    try {
      user = userDao.findUserByEmailAndIsValid(email, true);
    } catch (Exception ex) {
      ex.printStackTrace();
      result.setResult(true);
      result.setRes("服务异常,请稍后再试");
      return result;
    }
    if (user != null) {
      result.setResult(true);
      result.setRes("该邮箱地址已经注册");
      return result;
    }
    result.setResult(false);
    result.setRes("该邮箱地址尚未注册");
    return result;
  }

  @Override
  public Result<String> login(String email, String password) {
    Result<String> result = new Result<>();
    User user = userDao.findUserByEmailAndIsValid(email, true);
    if (user == null) {
      result.setResult(false);
      result.setRes("账号不存在,请先注册");
      return result;
    }
    try {
      if (user.comparePassword(password)) {
        result.setResult(true);
        result.setData(tokenService.getToken(email + password));
        tokenService.storageToken(email, result.getData());
        return result;
      }
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
      result.setResult(false);
      result.setRes("服务异常,请稍后再试");
      return result;
    }
    result.setResult(false);
    result.setRes("账号密码错误");
    return result;
  }
}
